﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Zero.UControl.Component
{
    /// <summary>
    /// InputNumber.xaml 的交互逻辑
    /// </summary>
    public partial class InputNumber : UserControl
    {
        public InputNumber()
        {
            InitializeComponent();
            MouseEnter += InputNumber_MouseEnter;
            MouseLeave += InputNumber_MouseLeave;
        }


        #region 依赖属性

        public double MinValue
        {
            get { return (double)GetValue(MinValueProperty); }
            set { SetValue(MinValueProperty, value); }
        }
       
        public static readonly DependencyProperty MinValueProperty =
            DependencyProperty.Register("MinValue", typeof(double), typeof(InputNumber), new PropertyMetadata(0d,ValidateCheck));

        public double MaxValue
        {
            get { return (double)GetValue(MaxValueProperty); }
            set { SetValue(MaxValueProperty, value); }
        }

        public static readonly DependencyProperty MaxValueProperty =
            DependencyProperty.Register("MaxValue", typeof(double), typeof(InputNumber), new PropertyMetadata(100d, ValidateCheck));

        public double Step
        {
            get { return (double)GetValue(StepProperty); }
            set { SetValue(StepProperty, value); }
        }

        public static readonly DependencyProperty StepProperty =
            DependencyProperty.Register("Step", typeof(double), typeof(InputNumber), new PropertyMetadata(1d, ValidateCheck));


        public double Number
        {
            get { return (double)GetValue(NumberProperty); }
            set { SetValue(NumberProperty, value); }
        }

        public static readonly DependencyProperty NumberProperty =
            DependencyProperty.Register("Number", typeof(double), typeof(InputNumber),
                new PropertyMetadata(1d, ValidateCheck));


        #endregion

        #region 依赖方法

        private static void ValidateCheck(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            string propName = e.Property.Name;
            if (!double.TryParse(e.NewValue.ToString(), out var value))
                throw new Exception("传入值类型解析错误");

            (d as InputNumber)?.DoValidateCheck(propName,value);
        }

        #endregion

        #region 本地方法
        private void DoValidateCheck(string propName,double value)
        {
            if (propName.Equals("Number"))
            {
                if(value < MinValue)
                {
                    SetCurrentValue(NumberProperty,MinValue);
                }
                else if(value > MaxValue)
                {
                    SetCurrentValue(NumberProperty, MaxValue);
                }
            }
            else if (propName.Equals("MinValue"))
            {
                if (value > MaxValue)
                    throw new Exception("请检查设置的范围区间是否合理");
            }
            else if (propName.Equals("MaxValue"))
            {
                if(value < MinValue)
                    throw new Exception("请检查设置的范围区间是否合理");
            }
            else if (propName.Equals("Step"))
            {
                if (value < 0)
                    throw new Exception("Step 值不能为负");
            }
        }
        #endregion

        #region 事件方法
        private void Sub_Button_Click(object sender, RoutedEventArgs e)
        {
            SetCurrentValue(NumberProperty,Number - Step);
        }

        private void Add_Button_Click(object sender, RoutedEventArgs e)
        {
            SetCurrentValue(NumberProperty, Number + Step);
        }

        private void InputNumber_MouseEnter(object sender, MouseEventArgs e)
        {
            VisualStateManager.GoToState(this, "MouseOver", true);
        }

        private void InputNumber_MouseLeave(object sender, MouseEventArgs e)
        {
            VisualStateManager.GoToState(this, "Normal", true);
        }
        #endregion
    }
}
